package gestoreImmagini;

import java.util.ArrayList;
import java.util.List;

import org.opencv.android.Utils;
import org.opencv.core.Mat;
import org.opencv.features2d.DescriptorExtractor;
import org.opencv.features2d.FeatureDetector;
import org.opencv.features2d.KeyPoint;

import android.graphics.Bitmap;
import android.util.Log;

public class Logo {

	final static int MAX_DIM = 250;
	
	private String nome;
	private String descrizione;
	private Bitmap logo;
	private Mat kpDescriptors;
	private List<KeyPoint> keyP;
	
	
	public Logo(String nome, String descrizione, Bitmap logo) {
		super();
		this.nome = nome;
		this.descrizione = descrizione;
		
		
		//salvo l'immagine ridotta
		int w, h;		
		if (logo.getWidth() >= logo.getHeight()){
			w = MAX_DIM;
			h = logo.getHeight()*MAX_DIM/logo.getWidth();
		}
		else{
			h = MAX_DIM;
			w = logo.getWidth()*MAX_DIM/logo.getHeight();
		}
		Log.d("HelloImageDetection", nome+": width="+w+", height="+h);
		
		logo = Bitmap.createScaledBitmap(logo, w, h, false);
		this.logo = logo.copy(Bitmap.Config.ARGB_8888, false);	

//		kpDescriptors = calcolaKpDescriptors(this.logo);
		
	}
	
	private List<KeyPoint> calcolaKeyPoint(Bitmap bm){
		//change bitmap config <- THAT'S THE POINT!
    	Bitmap img = bm.copy(Bitmap.Config.ARGB_8888, false);	    	
		Mat image = Utils.bitmapToMat(img);
		
		//detect keypoints
		List<KeyPoint> kp = new ArrayList<KeyPoint>();
		FeatureDetector fd = FeatureDetector.create(FeatureDetector.SURF);
		fd.detect(image, kp);
		Log.d("HelloImageDetection", nome+": "+kp.size()+"keypoints");
		
		return kp;
	}
	
	private Mat calcolaKpDescriptors(Bitmap bm){
		//change bitmap config <- THAT'S THE POINT!
    	Bitmap img = bm.copy(Bitmap.Config.ARGB_8888, false);	    	
		Mat image = Utils.bitmapToMat(img);

		List<KeyPoint> kp = getKeypoint();
		
		//compute descriptors
		Mat descriptors = new Mat();
		DescriptorExtractor de = DescriptorExtractor.create(DescriptorExtractor.SURF);
		de.compute(image, kp, descriptors);
		Log.d("HelloImageDetection", nome+": "+descriptors.rows()+"descriptors");
		
		return descriptors;
	}
	
	public Mat getKpDescriptors(){
		if (kpDescriptors.empty()) kpDescriptors = calcolaKpDescriptors(logo);
		return kpDescriptors;
	}
	
	public List<KeyPoint> getKeypoint(){
		if(keyP==null) keyP = calcolaKeyPoint(logo);
		return keyP;
	}
	
	public String getDescrizione() {
		return descrizione;
	}

	public void setDescrizione(String descrizione) {
		this.descrizione = descrizione;
	}

	public Bitmap getLogo() {
		return logo;
	}

	public void setLogo(Bitmap logo) {
		this.logo = logo;
	}

	public String getNome() {
		return nome;
	}

	public void setNome(String nome) {
		this.nome = nome;
	}


	

}
